<?php
/**
 * Created by PhpStorm.
 * User: shkj
 * Date: 2020/7/11
 * Time: 11:37
 */

namespace app\api\model;



use think\Model;
use think\facade\Request;
class Admin extends Model
{
    protected $autoWriteTimestamp = true;
    protected $createTime  = 'create_time';
    //开启自动设置
    protected $auto = [];//无论是新增还是更新都需要设置的字段
    //仅新增的有效
    protected $insert = ['create_tiame'];


    public function login($post){

        $admin = db('admin')->where('username' , $post['username'])->find();

        if($admin){
            $md5Password = md5($post['password']);

            if($admin['password'] == $md5Password){
                $token = $this->createToken($admin['username']);

                $updateData = array(
                    'token' => $token
                );

                db('admin')->where('username' , $post['username'])->update($updateData);

                $info = array(
                    'id' => $admin['id'],
                    'username' => $admin['username'],
                    'name' => $admin['name'],
                    'token' => $token
                );

                return_json(200 , '登录成功' , $info);
            }else{
                return_json(500 , '密码错误');
            }
        }else{
            return_json(500 , '用户不存在');
        }
    }

    private function createToken($username){
        $time = time();
        $oneMd5 = md5($time . $username);
        $twoMd5 = md5($time . $oneMd5) . $oneMd5;

        return $twoMd5;
    }
    public function select($pagenum=1,$pagesize=1){
      //全局查询条件
  

//实现搜素功能
  $map =[];
  $keywords = Request::param('query');
  if(!empty($keywords)){
    //条件2
   $map[] = ['username','like','%'.$keywords.'%'];
}
    
   	$res = db('admin')->where($map)->page($pagesize,$pagenum)->select();
    $count = self::count();
   
    $info = array(
      'data'=>$res,
      'total'=>$count
      );
    // $res = self::paginate(1);
   	return_json(200,'成功',$info);
   }

   public function add($data){
    $had = self::where('username',$data['username'])->find();
    if($had){
        return_json(400,'失败',"该账号已存在");
    }
   	$data['password'] = md5($data['password']);
   	$res = self::save($data);
   	if($res){
   		return_json(200,'成功',$res);
   	}else{
   		return_json(400,'失败',false);
   	}
   }

   public function status($data){

       if($data['type']==1){
        $status = 0;
       }else{
        $status = 1;
       }
       $res = self::where('id',$data['uid'])->update(['status'=>$status]);
       if($res){
         return_json(200,'成功',true);
        }else{
            return_json(400,'失败',false);
        }
   }

   public function del($id){
        $res = db('admin')->delete($id);
        if($res){
            return_json(200,'成功',true);
        }else{
            return_json(400,'失败',false);
        }
   }
   public function detail($id){
        $res = db('admin')->find($id);
       if($res){
           return_json(200,'成功',$res);
       }else{
           return_json(400,'失败',false);
       }
   }
   public function edit($id,$data){
       $res = db('admin')->where('id',$id)->where('password',$data['password'])->find();
       if(!$res){
        $data['password'] = md5($data['password']);
       }
       if(isset($data['imageUrls'])){
        $data['imageUrl'] = $data['imageUrls'];
        unset($data['imageUrls']);
       }
    
        $res = db('admin')->where('id',$id)->update($data);
       if($res){
           return_json(200,'成功',true);
       }else{
           return_json(400,'失败',false);
       }
   }

   public function delAll($ids){
     $res = self::destroy($ids);
     if($res){
           return_json(200,'成功',true);
       }else{
           return_json(400,'失败',false);
       }
   }

}